import router from './router'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from './store'
// 百名单
const while_list = ['/404', '/login']

// 路由前置守卫
router.beforeEach(async(to, from, next) => {
  // 开启进度条
  nprogress.start()

  const token = store.getters.token

  if (token) {
    if (to.path === '/login') {
      nprogress.done()
      next('/')
    } else {
      // 获取用户数据
      await store.dispatch('user/getUserInfo')
      next()
    }
  } else {
    if (while_list.includes(to.path)) {
      next()
    } else {
      nprogress.done()
      next('/login')
    }
  }
})

// 路由后置守卫
router.afterEach((to, from, next) => {
  // 关闭进度条
  nprogress.done()
})
